<?php
namespace TbfDatabase\SetTableData;

class MergeSql
{
    const BEFORE_TRANSACTION='before_transaction';
    const TRANSACTION='transaction';
    const AFTER_TRANSACTION='after_transaction';
    protected $query_list=array();
    public function query($sql,$data=null){
        return \TbfDatabase::execute($sql,$data);
    }
    public function addQuery($stage,$sql,$data=null)
    {
        $this->query_list[$stage][] = array($sql,$data);
    }
    public function addQueryBefore($stage,$sql,$data=null)
    {
        if (!empty($this->query_list[$stage])) {
            array_unshift($this->query_list[$stage],array($sql,$data));
        } else {
            $this->query_list[$stage][] = array($sql,$data);
        }
    }
    public function isEmptyQuery()
    {
        return empty($this->query_list);
    }
    public function runAllQuery()
    {
        $query_list = array();
        if (!empty($this->query_list[self::BEFORE_TRANSACTION])) {
            $query_list = array_merge($query_list,$this->query_list[self::BEFORE_TRANSACTION]);
        }
        if (!empty($this->query_list[self::TRANSACTION])) {
            $query_list[] = array('START TRANSACTION',null);
            $query_list = array_merge($query_list,$this->query_list[self::TRANSACTION]);
            $query_list[] = array('COMMIT',null);
        }
        if (!empty($this->query_list[self::AFTER_TRANSACTION])) {
            $query_list = array_merge($query_list,$this->query_list[self::AFTER_TRANSACTION]);
        }
        //不合并sql,保证能看到mysql的报错信息
        try {
            foreach($query_list as $row){
                $ret = $this->query($row[0],$row[1]);
            }
        } catch (\Exception $e) {
            $this->query('ROLLBACK');
            throw $e;
        }
        /*
        list($sql,$data) = $this->mergeSql($query_list);
        if (empty($data)) {
            $data = array();
        }
        try {
            if (!empty($sql)) {
                //$this->debugSql($sql,$data);
                $ret = $this->query($sql,$data);
            }
        } catch (\Exception $e) {
            $this->query('ROLLBACK');
            throw $e;
        }
        */
    }
    protected function mergeSql($query_list)
    {
        $all_sql = '';
        $all_data = array();
        foreach ($query_list as $row) {
            $sql = rtrim($row[0],';');
            $data = $row[1];
            $all_sql .= $sql.';';
            if (!empty($data)) {
                $all_data = array_merge($all_data,$data);
            }
        }

        return array($all_sql,$all_data);
    }
    /*
    protected function debugSql($sql,$data){
        if (empty($data)){
            echo $sql."\n";
            return;
        }
        foreach($data as $v1){
            $v1 = $this->connection->quote($v1);
            $pos = strpos($sql,'?');
            if ($pos===false){
                throw \Exception('? number not match data number');
            }
            $sql = substr($sql,0,$pos).$v1.substr($sql,$pos+1);
        }
        echo $sql."\n";
    }
    */
}
